package org.example.btchat;

import java.util.Collection;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

public class BluetoothDeviceAdapter extends ArrayAdapter<Object>
{
	private BluetoothAdapter bluetoothAdapter;
	private Context context;
	private int index_other = 1;
	private LayoutInflater inflater;
	
	public BluetoothDeviceAdapter(Context context)
	{
		super(context, 0);
		
		this.context = context;
		
		super.add(R.string.title_paired_devices);
		super.add(R.string.title_other_devices);
		
		bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
		if(bluetoothAdapter == null)
			throw new UnsupportedOperationException();
		
		for(BluetoothDevice device : bluetoothAdapter.getBondedDevices())
			addDevice(device);
		
		inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}
	
	@Override
	public void add(Object object)
	{
		throw new UnsupportedOperationException();
	}
	
	@Override
	public void addAll(Collection<? extends Object> collection)
	{
		throw new UnsupportedOperationException();
	}
	
	@Override
	public void addAll(Object... items)
	{
		throw new UnsupportedOperationException();
	}
	
	private void addDevice(BluetoothDevice device)
	{
		if(device.getBondState() == BluetoothDevice.BOND_BONDED)
		{
			super.insert(device, index_other);
			index_other++;
		}
		else
			super.add(device);
		
	}
	
	@Override
	public boolean areAllItemsEnabled()
	{
		return false;
	}
	
	public View getView(int position, View convertView, ViewGroup parent)
	{
		Object item = super.getItem(position);
		if(item instanceof BluetoothDevice)
			return getDeviceView((BluetoothDevice) item, convertView, parent);
		else
			return getHeader((Integer) item, convertView, parent);
	}
	
	@Override
	public int getViewTypeCount()
	{
		return 2;
	}
	
	@Override
	public void insert(Object object, int index)
	{
		throw new UnsupportedOperationException();
	}
	
	@Override
	public boolean isEnabled(int position)
	{
		Object item = getItem(position);
		if(item instanceof BluetoothDevice)
			return true;
		return false;
	}
	
	public void searchForDevices()
	{
		final BroadcastReceiver receiver = new BroadcastReceiver()
		{
			@Override
			public void onReceive(Context context, Intent intent)
			{
				String action = intent.getAction();
				
				if(BluetoothDevice.ACTION_FOUND.equals(action))
				{
					BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
					
					if(device.getBondState() != BluetoothDevice.BOND_BONDED)
						BluetoothDeviceAdapter.this.addDevice(device);
				}
				
				if(BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action))
					context.unregisterReceiver(this);
			}
		};
		
		IntentFilter filter = new IntentFilter();
		filter.addAction(BluetoothDevice.ACTION_FOUND);
		filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
		
		context.registerReceiver(receiver, filter);
		
		bluetoothAdapter.startDiscovery();
	}
	
	private View getDeviceView(BluetoothDevice item, View convertView, ViewGroup parent)
	{
		TextView tv;
		LinearLayout v = (LinearLayout) inflater.inflate(R.layout.list_item_device, parent, false);
		
		tv = (TextView) v.findViewById(R.id.list_name);
		tv.setText(item.getName());
		
		tv = (TextView) v.findViewById(R.id.list_address);
		tv.setText(item.getAddress());
		
		return v;
	}
	
	private View getHeader(int item, View convertView, ViewGroup parent)
	{
		TextView v = (TextView) inflater.inflate(R.layout.list_item_header, parent, false);
		
		v.setText(item);
		
		return v;
	}
}
